package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.service.inter.UserService;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.NursingServiceRecord;
import com.yunfeisoft.business.model.Room;
import com.yunfeisoft.business.service.inter.ElderService;
import com.yunfeisoft.business.service.inter.NursingServiceRecordService;
import com.yunfeisoft.business.service.inter.RoomService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: NursingServiceRecordController
 * Description: 护理服务记录表Controller
 * Author: Jackie liu
 * Date: 2020-05-22
 */
@Controller
public class NursingServiceRecordController extends BaseController {

    @Autowired
    private NursingServiceRecordService nursingServiceRecordService;

    @Autowired
    private ElderService elderService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private UserService userService;

    /**
     * 添加护理服务记录表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nursingServiceRecord/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(NursingServiceRecord record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "elderId", "老人为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        nursingServiceRecordService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改护理服务记录表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nursingServiceRecord/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(NursingServiceRecord record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "elderId", "老人为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        nursingServiceRecordService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询护理服务记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nursingServiceRecord/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        NursingServiceRecord record = nursingServiceRecordService.load(id);
        if (!ObjectUtils.isEmpty(record)) {
            Elder elder = elderService.load(record.getElderId());
            record.setElderName(elder.getName());
            record.setSex(elder.getSex());
            record.setNursingLevel(elder.getNursingLevel());
            Room room = roomService.load(elder.getRoomId());
            record.setRoomNum(room.getRoomNum());
            User user = userService.load(record.getCreateId());
            record.setCreateName(user.getName());
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询护理服务记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nursingServiceRecord/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String name = ServletRequestUtils.getStringParameter(request, "name", null);
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        if (!StringUtils.isBlank(name)) {
            params.put("elderName", name);
        }
        Page<NursingServiceRecord> page = nursingServiceRecordService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除护理服务记录表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/nursingServiceRecord/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        nursingServiceRecordService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
